from .models import UserProfile
from rest_framework import serializers
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import Group, Permission, ContentType


class ContentTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = ContentType
        fields = '__all__'


class PermSerializer(serializers.ModelSerializer):
    class Meta:
        model = Permission
        fields = '__all__'
    content_type = ContentTypeSerializer(read_only=True)


class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        # fileds = '__all__'
        fields = [
            'id', 'username', 'is_active', 'first_name', 'last_name', 'is_superuser', 'email', 'password', 'phone'
                    ]
        extra_kwargs = {
            'password': {'write_only': True},
            'username': {'min_length': 4, 'max_length': 16},
            'is_active': {'default': False},
            'is_superuser': {'default': False}
        }

    def validate_password(self, value):
        if 4 <= len(value) <= 16:
            return make_password(value)
        raise serializers.ValidationError("The length of password must between 4 and 16")
